Ext.define('MyDesktop.ProjectSourceWindow', {
    extend: 'Ext.ux.desktop.Module',

    requires: [
        'Ext.form.field.TextArea',
        'Ext.ux.statusbar.StatusBar'
    ],

    id:'',
    projectSourceControlUrl:'',
    windowTitle:'',

    init : function() {
        //MyDesktop.ProjectSourceWindow.superclass.init.call(this);
        this.launcher = {
            text: this.windowTitle,
            iconCls:'icon-grid',
            handler : this.createWindow,
            scope: this
        }
    },

    createWindow : function() {
        var id = this.id;
        var srcUrl = this.projectSourceControlUrl;
        var winId = id + '-win';
        var textAreaId = id + '-text-area';
        var statusBarId = id + '-status-bar';
        var title = this.windowTitle;

        function updateStatusBar(busyText) {
            var sb = Ext.getCmp(statusBarId);
            if (busyText) {
                sb.setStatus({
                    text: busyText,
                    iconCls: sb.busyIconCls
                });
            } else {
                sb.clearStatus({useDefaults:true});
            }
        }

        var paracol = this.app.paracol;
        var desktop = this.app.getDesktop();
        var win = desktop.getWindow(winId);
        if (!win) {

            function loadText() {
                updateStatusBar("Loading ...");
                paracol.postCustomMessage("GET", srcUrl + '/dump', {
                    onResponse: function(response) {
                        Ext.getCmp(textAreaId).setValue(response);
                        updateStatusBar();
                    }
                })
            }

            function storeText() {
                var storeIt = confirm("Really store the project source?");
                if (storeIt) {
                    updateStatusBar("Saving ...");
                    var projectSource = Ext.getCmp(textAreaId).getValue();
                    paracol.postCustomMessage("POST", srcUrl + "/store", {
                        onResponse: function(response) {
                            //alert("Project source stored");
                            updateStatusBar();
                        }
                    }, projectSource)
                }
            }

            var sbar = new Ext.ux.StatusBar({
                //renderTo: 'basic',
                id: statusBarId,
                //width: 550,
                // defaults to use when the status is cleared:
                defaultText: 'Ready',
                //defaultIconCls: 'default-icon',

                // values to set initially:
                text: 'Ready',
                iconCls: 'x-status-valid',

                items: []
            });

            win = desktop.createWindow({
                id: winId,
                title: title,
                width: 740,
                height: 480,
                iconCls: 'icon-grid',
                shim: false,
                animCollapse: false,
                constrainHeader: true,

                bbar: sbar,

                layout: 'fit',
                items:
                        new Ext.Panel({
                            layout:'anchor',
                            anchorSize: {width:800, height:600},
                            border:false,

                            viewConfig: {
                                forceFit:true
                            },
                            //autoExpandColumn:'company',

                            items: [
                                {
                                    xtype: 'textarea',
                                    id: textAreaId,
                                    name: 'description',
                                    anchor: '100% 100%',
                                    hideLabel: true
                                }
                            ],

                            tbar:[
                                {
                                    text:'Save Project',
                                    tooltip:'Save Project',
                                    iconCls:'add',
                                    handler: storeText

                                },
                                '-',
                                {
                                    text:'Refresh',
                                    tooltip:'Refresh',
                                    iconCls:'option',
                                    handler: loadText
                                }
                            ]
                        })
            });

            loadText();

        }
        win.show();
    }
});